package io.kiki.sba.registry.lifecycle.impl;

import io.kiki.sba.registry.exception.DisposeException;
import io.kiki.sba.registry.exception.InitializeException;
import io.kiki.sba.registry.exception.StartException;
import io.kiki.sba.registry.exception.StopException;
import io.kiki.sba.registry.lifecycle.Lifecycle;


public final class LifecycleHelper {
    private LifecycleHelper() {
    }

    public static void initializeIfPossible(Object obj) throws InitializeException {

        if (obj instanceof Lifecycle && ((Lifecycle) obj).getLifecycleState().canInitialize()) {
            ((Lifecycle) obj).initialize();
        }
    }

    public static void startIfPossible(Object obj) throws StartException {

        if (obj instanceof Lifecycle && ((Lifecycle) obj).getLifecycleState().canStart()) {
            ((Lifecycle) obj).start();
        }
    }

    public static void stopIfPossible(Object obj) throws StopException {

        if (obj instanceof Lifecycle && ((Lifecycle) obj).getLifecycleState().canStop()) {
            ((Lifecycle) obj).stop();
        }
    }

    public static void disposeIfPossible(Object obj) throws DisposeException {
        if (obj instanceof Lifecycle && ((Lifecycle) obj).getLifecycleState().canDispose()) {
            ((Lifecycle) obj).dispose();
        }
    }
}
